<?php

namespace App\Library\Service;

use App\Library\Logic\AuctionLogic;
use App\Library\Logic\AuditLogic;
use App\Library\Logic\UserDmLogic;
use App\Library\Requests\Audit\RealNameAuthAddRequest;
use Illuminate\Http\JsonResponse;

class AuditService extends BaseService
{
    /**
     * @param AuditLogic $logic
     */
    public function __construct(AuditLogic $logic)
    {
        $this->logic = $logic;
    }

    /**
     *
     * @param mixed $data
     * @param string $type
     * @return JsonResponse
     *
     */
    public static function helperAudit(mixed $data, string $type): JsonResponse
    {
        $audit = AuditLogic::query()->where(['user_id' => USERID, 'type' => $type])->first();
        if (!is_null($audit) && $audit->status === 0) return fail(637);
        else if (!is_null($data)) {
            $audit = AuditLogic::insertForModel(['userId' => USERID, 'type' => $type, 'content' => $data]);
            if (empty($audit->id)) return fail(434);
        }
        return success();
    }

    /**
     * 实名认证
     * @param RealNameAuthAddRequest $request
     * @return JsonResponse
     */
    public function realNameAuth(RealNameAuthAddRequest $request): JsonResponse
    {
        $where = ['userId' => USERID, 'type' => __FUNCTION__];
        $status = $this->logic->valueByWhere(array_merge($where, ['orderByDesc' => null]), null, 'status');
        if (!is_null($status)) {
            if ($status === 1) return failMessage('正在审核中');
            else if ($status === 2) return failMessage('已经认证成功');
        }
        $this->logic->insertForModel(array_merge($where, ['content' => handleRequest($request)]));
        UserDmLogic::updateByWhere(['authStatus' => 1], 'userId', USERID);
        return success();
    }
}
